package com.sojson.project.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.core.EGeneralYesNo;
import com.sojson.project.controller.param.verify.LeaveWordParamVerify;
import com.sojson.project.entity.po.LeaveWord;
import com.sojson.project.service.LeaveWordService;
import com.sojson.result.Result;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 留言表控制器
 * 
 * @author liu
 * @date 2021-10-27
 */
@Api(tags = "留言表管理")
@RestController
@RequestMapping("/leaveWord")
public class LeaveWordController {

    @Autowired
    private LeaveWordService leaveWordService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation("给留言表添加一条数据")
    public Result<Integer> insert(@RequestBody LeaveWord dto) {
        // 校验参数
        Result<Integer> paramResult = LeaveWordParamVerify.insert(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.insertInit();
        return Result.success(leaveWordService.insert(dto));
    }

    /**
     * 批量插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/leaveWord/insertList')")
    @ApiOperation("给留言表添加多条数据")
    public Result<Integer> insertList(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.insertList(dto));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param dto
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/leaveWord/deleteByDto')")
    @ApiOperation("通过对象批量删除留言表数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.deleteByDto(dto));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/leaveWord/countAll')")
    @ApiOperation("统计留言表全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(leaveWordService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/leaveWord/listAll')")
    @ApiOperation("查询留言表所有数据(包含逻辑删除的)")
    public Result<List<LeaveWord>> listAll() {
        return Result.success(leaveWordService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param dto
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/leaveWord/countByDto')")
    @ApiOperation("根据对象统计留言表数据")
    public Result<Integer> countByDto(LeaveWord dto) {
        return Result.success(leaveWordService.countByDto(dto));
    }

    /**
     * 根据对象查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/leaveWord/listByDto')")
    @ApiOperation("根据对象查询留言表数据")
    public Result<List<LeaveWord>> listByDto(LeaveWord dto) {
        return Result.success(leaveWordService.listByDto(dto));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/leaveWord/deleteById/{id}')")
    @ApiOperation("通过id删除留言表数据(物理删除)")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(leaveWordService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/leaveWord/deleteById')")
    @ApiOperation("通过id数组删除留言表数据(物理删除)")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(leaveWordService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/leaveWord/updateAllById')")
    @ApiOperation("通过id更新留言表数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.updateAllById(dto));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/leaveWord/updateAllByIdList')")
    @ApiOperation("通过id列表更新留言表数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.updateAllByIdList(dto));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/leaveWord/updateDtoListAllById')")
    @ApiOperation("通过id列表更新留言表数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.updateDtoListAllById(dto));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/leaveWord/updateById')")
    @ApiOperation("通过id更新留言表数据(局部更新)")
    public Result<Integer> updateById(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.updateById(dto));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/leaveWord/updateByIdList')")
    @ApiOperation("通过id列表更新留言表数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.updateByIdList(dto));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/leaveWord/updateDtoListById')")
    @ApiOperation("通过id列表更新留言表数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody LeaveWord dto) {
        return Result.success(leaveWordService.updateDtoListById(dto));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/leaveWord/findById')")
    @ApiOperation("通过id查询留言表数据")
    public Result<LeaveWord> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(leaveWordService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/leaveWord/findByIds/{ids}')")
    @ApiOperation("通过id数组查询留言表数据")
    public Result<List<LeaveWord>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(leaveWordService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param dto
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/leaveWord/findIdByDto')")
    @ApiOperation("通过对象查询留言表id列表")
    public Result<List<Integer>> findIdByDto(LeaveWord dto) {
        return Result.success(leaveWordService.findIdByDto(dto));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/leaveWord/clear')")
    @ApiOperation("清空留言表数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(leaveWordService.clear());
    }

    /**
     * 通过id更新状态(局部更新)
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @PutMapping("/changeStatus")
    @PreAuthorize("@ss.hasPermi('/leaveWord/changeStatus')")
    @ApiOperation("通过id更新留言表状态数据(局部更新)")
    public Result<Integer> changeStatus(@RequestBody LeaveWord dto) throws IOException {
        // 校验参数
        Result<Integer> paramResult = LeaveWordParamVerify.changeStatus(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        LeaveWord leaveWord = new LeaveWord();
        leaveWord.updateInit();
        leaveWord.setId(dto.getId());
        Integer status = dto.getStatus();
        leaveWord.setStatus(status);
        if (status == EGeneralYesNo.YES.getCode()) {
            leaveWord.setNoUserOper(TokenUtil.getUserId());
            leaveWord.setNameUserOper(TokenUtil.getUserName());
        }
        return Result.success(leaveWordService.changeStatus(leaveWord));
    }

}